/*
 *  $Id: PlayerHands.cs 1005 2010-05-09 16:51:22Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib.Geometry;

namespace Ragima {
    /// <summary>
    /// Содержимое таба, определяющее карты в руках игрока
    /// </summary>
    public class PlayerHandsTabContent: TabContent {
        private Player _player;
        private List<Region> _regions;
        private PlayerHandCardRegion _selected;

        public PlayerHandsTabContent(Player player): base(TabType.Cards, false) {
            _player = player;
            _regions = new List<Region>();
            for (int i = 0; i < Constants.FieldSize; i++)
                _regions.Add(new PlayerHandCardRegion(Constants.GetRowRect(i, 3), this, i));
        }

        public static PlayerHandsTabContent GetContent(Tabs tabs, Player player) {
            PlayerHandsTabContent content = null;
            if (tabs != null) content = tabs.FindContent<PlayerHandsTabContent>(delegate(PlayerHandsTabContent other) {
                return other.Player == player;
            });
            return content != null ? content : new PlayerHandsTabContent(player);
        }

        public Player Player { get { return _player; } }

        private TargetRequest _request = null;
        public PlayerHandCardRegion Selected {
            get { return _selected; }
            set {
                _selected = value;
                if (value != null) {
                    _request = new HandCardTargetRequest(value);
                    Game.Current.SetTarget(_request);
                } else if (Game.Current.HasTarget && _request == Game.Current.Target.Request) {
                    Game.Current.ResetTarget();
                    _request = null;
                }
                Game.Current.UpdateRegions();
            }
        }

        public override int Count {
            get { return _player.Hands.Count; }
        }

        public override IEnumerable<Region> GetRegions() {
            for (int i = 0; i < CountFromPosition; i++)
                yield return _regions[i];
        }

        public override Region SelectedRegion { get { return _selected; } }

        public override void OnSelect() {
            if (Game.Current.HasTarget && Game.Current.Target.IsRealTarget) return;
            Selected = null;
        }
        public override void OnDeselect() {
            if (Game.Current.HasTarget && Game.Current.Target.IsRealTarget) return;
            Selected = null;
        }
        public override void OnScroll() {
            Selected = null;
        }

        public HandCard GetCard(int num) {
            int index = GetIndex(num);
            return (index >= 0 && index < Count) ? _player.Hands[index] : null;
        }
    }

    /// <summary>
    /// Регион, определяющий карту в руке игрока
    /// </summary>
    public class PlayerHandCardRegion: HelpRegion {
        private PlayerHandsTabContent _content;
        private int _num;

        public PlayerHandCardRegion(Rectangle rect, PlayerHandsTabContent content, int num): base(rect) {
            _content = content;
            _num = num;
        }

        public Player Player { get { return _content.Player; } }
        public HandCard Card { get { return _content.GetCard(_num); } }
        public bool Selected {
            get { return _content.Selected == this; }
            set { _content.Selected = value ? this : null; }
        }

        public bool CanUse(bool notice) {
            if (!Player.TestCurrent(notice)) return false;
            HandCard card = Card;
            if (card == null) return false;
            return card.Card.TestConditionsFor(Player, notice);
        }

        public override void Draw() {
            HandCard card = Card;
            if (card == null) return;
            if (card.Opened) {
                card.Card.DrawSmall(Rect.X, Rect.Y);
                if (!CanUse(false))
                    Data.CardDisabled.Draw(Rect.Location);
                if (Selected)
                    Data.CardSelect2.Draw(Rect.Location);
            } else
                Data.Cardback.Draw(Rect.X, Rect.Y);
        }
        public override void DrawTarget() {
            Draw();
            Data.CardSelect1.Draw(Rect.Location);
        }

        public override void OnClick() {
            HandCard card = Card;
            if (card == null) return;
            if (card.Opened) {
                if (Selected) {
                    Selected = false;
                } else {
                    if (CanUse(true)) {
                        Data.Sounds.Events.Play("card.choose");
                        Selected = true;
                    }
                }
            } else if (Player.TestCurrent(true)) {
                Player.Game.Mode.AddEvent(new Events.OpenCard(Player, card));
                 Data.Sounds.Events.Play("card.flip");
                _content.Selected = null;
            }
        }

        public override IHelpProvider HelpObject {
            get {
                HandCard card = Card;
                return card != null && card.Opened ? card.Card : null;
            }
        }
    }
}